#pragma once

#include "noncopyable.h"

#include <functional>
#include <thread>
#include <memory>
#include <unistd.h>
#include <string>
#include <atomic>

class Thread : noncopyable
{
public:
    using ThreadFunc = std::function<void()>;

    explicit Thread(ThreadFunc, const std::string& name = std::string());
    ~Thread();

    void start();
    void join();

    bool started() const {return started_;}
    pid_t tid() const {return tid_;}
    const std::string& name() const {return name_;}

    static int numCreated() {return numCreated_;}

private:
    void setDefaultName();

    bool started_;     // 线程是否已经启动
    bool joined_;      // 是否调用过 join()
    std::shared_ptr<std::thread> thread_; // 线程对象指针
    pid_t tid_;        // 线程的真实 ID（Linux 下）
    ThreadFunc func_;  // 线程要执行的函数
    std::string name_; // 线程名称
    static std::atomic_int numCreated_; // 静态计数器，记录总共创建了多少个 Thread 对象
};